package com.dongyimai.oauth.util;

import com.alibaba.fastjson.JSON;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateCrtKey;
import java.util.HashMap;
import java.util.Map;

public class AdminToken {



    public static String createAdminToken() {
        ClassPathResource resource = new ClassPathResource("dongyimai.jks");

        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, "dongyimai".toCharArray());

        //密钥对
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("dongyimai", "dongyimai".toCharArray());

        RSAPrivateCrtKey privateCrtKey = (RSAPrivateCrtKey) keyPair.getPrivate();

        Map<String, Object> map = new HashMap<>();

        map.put("name", "aaa");
        map.put("address", "xa");
        map.put("authorities", new String[]{"admin", "user"});
        Jwt jwt = JwtHelper.encode(JSON.toJSONString(map), new RsaSigner(privateCrtKey));
        String token = jwt.getEncoded();
        return token;
    }
}
